var sfid="";
$(function() {


    mini.parse();
    var tree1 = mini.get("canhuirenyuan");
    var tree2 = mini.get("quexirenyuan");
    $.post(SVR_PATH+"cqzd/do/org/organization/listAllTreeNode/cqzd",null,function(e){
        tree2.loadList(mini.clone(e.data), "id", "pid");
        tree1.loadList(e.data, "id", "pid");
    });

    if (window.top === window.self) { // are you trying to put self in an iframe?
        var getParameters = function (url) {
            var nameValues = [];
            if (url) {
                if (url.indexOf('?') == -1) {
                    return nameValues.push({ baseUrl: url });
                };
                var arrUrlParts = url.split('?');
                var baseUrl = arrUrlParts[0];
                var urlParameters=arrUrlParts[1].split('&');
                for (var i = 0; i < urlParameters.length; i++) {
                    var nameValue = urlParameters[i].split('=');
                    nameValues.push({ baseUrl: baseUrl, key: nameValue[0], urlvalue: nameValue[1]});
                };
                return nameValues;
            };
        };
        var sysid=getParameters(window.location.href)[0].urlvalue;
        $('.thisrightselect').val(sysid)
        loadrightlist(sysid);

    }else{
        //右侧菜单栏加载
        $.ajax({
            type: "POST",
            dataType: "json",
            url:  SVR_PATH+"cqzd/do/diacinfo/queryDiacrisisInfo",
            data: {
                'key': '1',
                "isValid": '1'
            },
            success: function(result) {
                var ghlist = result;
                $('.xinruguahao').append($("#user").tmpl(ghlist));
            }
        });
        $.ajax({
            type: "POST",
            dataType: "json",
            url:  SVR_PATH+"cqzd/do/dibinfo/queryListByKey",
            data: {
                'key': '1'
            },
            success: function(result) {
                var ghlist = result;
                $('.lishiguahao').append($("#lishiuser").tmpl(ghlist));
            }
        });
    }
});



var rightmenushow = true;



//右侧效果
$('.zdmenubuttom').click(function() {
    if (rightmenushow) {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        rightmenushow = false;
        $('.zdmenubuttom').children("img").attr("src","../img/hide.png")
    } else {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        $('.zdmenubuttom').children("img").attr("src","../img/show.png")
        rightmenushow = true;
    }
});
//右侧菜单点击
function rightuserclick(el) {
    $(".userclick").removeClass("userclick");
        $(el).addClass("userclick")
    $(".zhezhao").fadeOut(1000);
    var userid = $(el).children('.thisuserid').html();
    var username = $(el).find('.thisusername').html();
    patientCardId = $(el).find('.patientCardId').html();
    sfid="";
    var form = new mini.Form("#HZXXFORM");
    form.clear();
    var patient=mini.get("#patientName");
    var diacrisisId=mini.get("#diacrisisId");
    patient.setValue(username);
    diacrisisId.setValue(userid);
}

//点击其他地方隐藏右侧
$(".bigdiv").click(function() {
    if (rightmenushow) {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        rightmenushow = false;
    }
})
//病历查询用到此变量
var binliDibId="";

function rightlishiclick(el) {
    $(".userclick").removeClass("userclick");
        $(el).addClass("userclick")
    $(".zhezhao").fadeOut(1000);
    var userid = $(el).children('.thisuserid').html();    
    sfid=userid;
    binliDibId=userid;
    $.ajax({
        type: "get",
        
        dataType: "json",
        url: SVR_PATH+"cqzd/do/dibinfo/get/" + userid,
        data: {},
        success: function(result) {
            patientCardId= result.data.dibAndDiacModel.patientCardId
            var form = new mini.Form("#HZXXFORM");
            form.setData(result.data);
            // for(var i=0;i<result.data.dibEmp.length;i++){
            //     if(result.data.dibEmp[i].isAbsence==="0"){
            //         canhui+=result.data.dibEmp[i].empId+","
            //     }
            //     else if(result.data.dibEmp[i].isAbsence==="1"){
            //         weidao+=result.data.dibEmp[i].empId
            //     }
            // }
            var canhui=mini.get("canhuirenyuan");
            var quexi = mini.get("quexirenyuan");
            var checkIdArr = [],checkTextArr=[];
            var checkQXid=[],checkqxText=[];
            var nodes = canhui.tree.findNodes(function(node){
                for (var i = 0; i < result.data.dibEmp.length; i++) {  
                    if(node.id === "emp-"+result.data.dibEmp[i].empId&&result.data.dibEmp[i].isAbsence!="0"){
                        checkIdArr.push(node.id);
                        checkTextArr.push(node.text);
                        return true;  
                    }
                }  
                return false;  
            }); 
            var quexinode = canhui.tree.findNodes(function(node){  
                for (var i = 0; i < result.data.dibEmp.length; i++) {  
                    if(node.id === "emp-"+result.data.dibEmp[i].empId&&result.data.dibEmp[i].isAbsence!="1"){
                        checkQXid.push(node.id);
                        checkqxText.push(node.text);
                        return true;  
                    }
                }  
                return false;  
            }); 
            
            canhui.setValue(checkIdArr.join(","));
            canhui.setText(checkTextArr);
            canhui.tree.checkNodes(nodes);    
            quexi.setValue(checkQXid.join(","));
            quexi.setText(checkqxText);
            quexi.tree.checkNodes(quexinode);    
        }
    });
}
//提交
function submintZHXXGLform(_this){
    var loadingWin =mini.loading("请稍后...", "等待");
    var buttomtext=$(_this).val();
    mini.parse();
    var canhui=mini.get("canhuirenyuan").getCheckedNodes ( false );
    var quexi=mini.get("quexirenyuan").getCheckedNodes ( false );
    var dibEmp=[];
    
    for(var i=0;i<canhui.length;i++){
        var yisheng1={
            "empId":"",
            "empName":"",
            "orgId":"",
            "orgName":"",
            "isAbsence":""
        }
        yisheng1.empId=canhui[i].realId;
        yisheng1.empName=canhui[i].text;
        yisheng1.orgId= (canhui[i].pid).substring(4);
        yisheng1.orgName=canhui[i].ptext;
        yisheng1.isAbsence="1";
        dibEmp.push(yisheng1);
    }
    for(var i=0;i<quexi.length;i++){
        
        var yisheng2={
            "empId":"",
            "empName":"",
            "orgId":"",
            "orgName":"",
            "isAbsence":""
        }
        yisheng2.empId=quexi[i].realId;
        yisheng2.empName=quexi[i].text;
        yisheng2.orgId= (quexi[i].pid).substring(4);
        yisheng2.orgName=quexi[i].ptext;
        yisheng2.isAbsence="0";
        dibEmp.push(yisheng2);
    }
    var form = new mini.Form("#HZXXFORM");
    form.validate();
	if (form.isValid() == false){
            mini.hideMessageBox(loadingWin);
			mini.alert("请完善您的数据")
			return;
	};
    var biglist = form.getData();
    
    biglist.dibEmp=dibEmp;
    biglist.dibAndDiacModel.dibId=sfid;
    var fileArray=document.querySelector('#thisnibaba');
    var formData = new FormData();
    var json = JSON.stringify(biglist);
    formData.append("json",json);
    for(var i=0,f;f=fileArray.files[i];i++){
        formData.append("file", f);
    }
    if(buttomtext==="保存"){
        if(sfid===""){
            $.ajax({
                url: SVR_PATH+"cqzd/do/dibinfo/addDibInfo",
                data: formData,
                type: "POST",
                processData: false,
                contentType: false,
                success: function(data) {
                    mini.hideMessageBox(loadingWin);
                    if(data.errMsg){
                        mini.alert("新增失败,"+data.errMsg);
                        return;
                    }
                    mini.alert("新增成功")
                    setTimeout(function () {
                        location.reload();
                    },500);
                },
                error: function(e) {
                    mini.hideMessageBox(loadingWin);
                    mini.alert("新增失败")
                }
            });
        }else{
            $.ajax({
                url: SVR_PATH+"cqzd/do/dibinfo/updateDibInfo",
                data: formData,
                type: "POST",
                processData: false,
                contentType: false,
                success: function(data) {
                    mini.hideMessageBox(loadingWin);
                    if(data.errMsg){
                        mini.alert("修改失败,"+data.errMsg);
                        return;
                    }
                    mini.alert("修改成功");
                },
                error: function(e) {
                    mini.hideMessageBox(loadingWin);
                    mini.alert("修改失败")
                }
            });
        }
    }else if(buttomtext==="导出PDF"){
        HttpUtil.standardPost(SVR_PATH+"cqzd/do/dibinfo/getPdf",{json:json});
    }
}

$('.righttitle').click(function() {
    $(this).next().animate({
        height: 'toggle'
    }, 350);
    
})

function changeselect(){
    var selectkey=$(".thisrightselect").val();
    loadrightlist(selectkey);
}

function loadrightlist(selectkey){
    $.ajax({
        type: "POST",
        dataType: "json",
        url:  SVR_PATH+"cqzd/do/diacinfo/queryDiacrisisInfo",
        data: {
            'key': selectkey,
            "isValid": '1'
        },
        success: function(result) {
            var ghlist = result;
            $('.xinruguahao').empty();
            $('.xinruguahao').append($("#user").tmpl(ghlist));
        }
    });
    $.ajax({
            type: "POST",
            dataType: "json",
            url:  SVR_PATH+"cqzd/do/dibinfo/queryListByKey",
            data: {
                'key': selectkey
            },
        success: function(result) {
            var ghlist = result;
            $('.lishiguahao').empty();
            $('.lishiguahao').append($("#lishiuser").tmpl(ghlist));
        }
    });
}



//打开病例管理
function onclick4() {
    $(".tanchuang").css("display", "flex");
    loadwjxx(binliDibId)
}
//关闭病例管理
function closeBLZL() {
    $(".tanchuang").css("display", "none");
}
//点击病例查询
function loadwjxx(binliDibId) {
    $.ajax({
        type: "get",
        dataType: "json",
        url: SVR_PATH+"cqzd/do/dibfile/getFileByFkId/" + binliDibId,
        data: {},
        success: function(result) {
            var files_grid = mini.get("files_grid");
            files_grid.setData(result.data)
        }
    });
}
//病例管理里面的a标签
function onActionRenderer(e) {
    var row = e.row;
    var aa = row.fileName;
    var s = '<a href="'+SVR_PATH+'cqzd/do/dibfile/getFileByCode/' + row.fileCode + '">' + aa + '</a>';
    return s;

}

//身份证
var patientCardId;

//导出
function exportThis() {
    window.open('/cqzd/Consultation/selConsultationAll?patientCardId='+patientCardId)
}